package cn.xd.itstack.springframework.core.io;

import org.springframework.util.Assert;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Classname DefaultResourceLoader
 * @Description TODO
 * @Date 2022/5/16 下午2:16
 * @Created by lvxiaodong
 */
public class DefaultResourceLoader implements ResourceLoader {


    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "location must not be null");
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {

            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        } else {
            try {
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return new FileResource(location);
            }

        }
    }
}
